package com.moonLight.weblog.common.domain.mapper;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.moonLight.weblog.common.domain.dos.CategoryDO;
import com.moonLight.weblog.common.domain.dos.TagDO;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

public interface TagMapper extends BaseMapper<TagDO> {
    default Page<TagDO> selectPage(Long pageNum, Long pageSize, String name, LocalDate startTime, LocalDate endTime) {
        Page<TagDO> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<TagDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(name), TagDO::getName, name) //包括""格式也不行
                .ge(Objects.nonNull(startTime), TagDO::getCreateTime, startTime) //大于等于开始时间
                .le(Objects.nonNull(endTime), TagDO::getCreateTime, endTime) //小于等于结束时间
                .orderByDesc(TagDO::getCreateTime);

        return selectPage(page, wrapper);
    }

    /**
     * 根据标签 ID 批量查询
     * @param tagIds
     * @return
     */
    default List<TagDO> selectByIds(List<Long> tagIds) {
        return selectList(Wrappers.<TagDO>lambdaQuery()
                .in(TagDO::getId, tagIds));
    }

    default List<TagDO> selectByLimit(Long limit){
        return selectList(Wrappers.<TagDO>lambdaQuery()
                .last(String.format("LIMIT %d", limit)));
    }

}
